package com.jjb.www.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.hjq.base.BaseAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.view.ShapeButton;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.http.api.InvoiceListApi;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.ui.adapter.InvoiceListAdapter;
import com.jjb.www.widget.CommonRefreshView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * author : Android CRG
 * time   : 2019/03/01
 * desc   : 申请电子发票列表
 */
public class InvoiceListActivity extends AppActivity implements BaseAdapter.OnChildClickListener {
    private InvoiceListAdapter mAdapter;
    private CommonRefreshView mRefreshView;
    private ShapeButton mBtnGo;
    private BigDecimal bigDecimal = new BigDecimal(0);
    List<InvoiceListApi.Bean> list;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_invoice_list;
    }

    @Override
    protected void initView() {
        mAdapter = new InvoiceListAdapter(getContext());
        mRefreshView = findViewById(R.id.refreshView);
        mBtnGo = findViewById(R.id.btn_go);
        mAdapter.setOnChildClickListener(R.id.invoice_item, this);
        setOnClickListener(mBtnGo);
    }

    @Override
    public void onClick(View view) {
        if (view == mBtnGo) {
            if (bigDecimal.compareTo(new BigDecimal(0)) == 1) {
                Intent intent = new Intent();
                intent.putExtra("price", bigDecimal + "");
                intent.setClass(getContext(), InvoiceActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    protected void initData() {
        mRefreshView.setDataHelper(new CommonRefreshView.DataHelper<InvoiceListApi.Bean>() {
            @Override
            public AppAdapter<InvoiceListApi.Bean> getAdapter() {
                return mAdapter;
            }

            @Override
            public void loadData(int p, HttpCallback<HttpListData<InvoiceListApi.Bean>> callback) {
                EasyHttp.get(InvoiceListActivity.this)
                        .api(new InvoiceListApi()
                                .setPage(p)
                                .setPageSize(mAdapter.getSize())
                        ).request(new HttpCallback<HttpListData<InvoiceListApi.Bean>>(InvoiceListActivity.this) {

                    @Override
                    public void onStart(Call call) {

                    }

                    @Override
                    public void onSucceed(HttpListData<InvoiceListApi.Bean> result) {
                        callback.onSucceed(result);

                    }

                    @Override
                    public void onFail(Exception e) {
                        super.onFail(e);
                        callback.onFail(e);
                    }

                    @Override
                    public void onEnd(Call call) {
                        super.onEnd(call);
                        callback.onEnd(call);
                    }
                });
            }

            @Override
            public void onRefreshSuccess(List<InvoiceListApi.Bean> list, int listCount) {

            }

            @Override
            public void onRefreshFailure() {

            }

            @Override
            public void onLoadMoreSuccess(List<InvoiceListApi.Bean> loadItemList, int loadItemCount) {

            }

            @Override
            public void onLoadMoreFailure() {

            }
        });
        mRefreshView.initData();
    }

    @Override
    public void onChildClick(RecyclerView recyclerView, View childView, int position) {
        list = new ArrayList<>();
        list = mAdapter.getData();
        //0可开票 1开票中 2已开票 3开票失败  4开票过期
        if (childView.getId() == R.id.invoice_item) {
            if (mAdapter.getItem(position).getInvoice_status() == 0 || mAdapter.getItem(position).getInvoice_status() == 3) {
                for (InvoiceListApi.Bean bean : list) {
                    if (bean.getSelect()) {
                        bean.setSelect(false);
                    } else {
                        bean.setSelect(true);
                    }
                }
                mAdapter.setData(list);
            }
            bigDecimal = new BigDecimal(0);
            for (InvoiceListApi.Bean bean : mAdapter.getData()) {
                if (bean.getSelect()) {
                    BigDecimal parce = new BigDecimal(bean.getPay_money());
                    bigDecimal = bigDecimal.add(parce);
                }
            }

            if (bigDecimal.compareTo(new BigDecimal(0)) == 0) {
                mBtnGo.getShapeDrawableBuilder()
                        .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_F1F1F1))
                        .intoBackground();
            } else {
                mBtnGo.getShapeDrawableBuilder()
                        .setSolidColor(ContextCompat.getColor(getContext(), R.color.color_3675FE))
                        .intoBackground();
            }
            mBtnGo.setText("¥" + bigDecimal + "申请发票");
        }

    }
}